unit clsSenha;

interface

uses dbtables, Sysutils, dialogs, dm, SecHash, biblio, Registry, classes, windows, clsContratante;

type
  Tsenha = class(TObject)
  public
    cod: string;
    data_exp: tdatetime;
    data_atual: tdatetime;
    dias: string;
    function Entrada: Boolean;
    function ver_senha(senha: string): boolean;
    function ver_data: byte;
    function altera_data_exp: boolean;
    function altera_data_atual: boolean;
    function altera_checkSum: boolean;
    function grava_CheckSum: boolean;
    procedure Cria_Chave_Registro;
    function Verifica_Chave_Registro: boolean;
  end;

implementation

uses DB;

var
  contratante: TContratante;

function Tsenha.Entrada;
var
  tq: TQuery;
begin
  tq := TQuery.create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from Contratante');
  tq.open;
  if tq.FieldByName('Ultima_Senha').asstring = 'True' then result := true else result := false;
  tq.close;
  tq.free;
end;

function Tsenha.ver_senha;
var
  num: integer;
  senha1: string;
  tq: TQuery;
  sh: TSecHash;
  tipo: TIntDigest;
  ch: pchar;
  ch1: char;
  senha2: string;
  xcgc: string;
begin
  dmint.db.StartTransaction;
  result := false;
  try
    tq := tquery.create(dmint);
    tq.databasename := dmint.db.databasename;
    tq.active := False;
    tq.SQL.clear;
    tq.params.clear;
    tq.SQL.add('Select * from Contratante');
    tq.open;
    xcgc := tq.fieldbyname('cgc').asstring;
    senha1 := tq.fieldbyname('cgc').asstring + datetostr(date);
    senha2 := '';
    for num := 1 to length(senha1) do begin
      ch := pchar(copy(senha1, num, 1));
      ch1 := ch[0];
      senha2 := senha2 + chr(ord(ch1) + 1);
    end;
    sh := TSecHash.Create(dmint);
    tipo := sh.ComputeString(senha2);
    if senha = (intToHex(tipo[0], 8) + intToHex(tipo[4], 8)) then begin
      tq.close;
      tq.SQL.clear;
      tq.Params.clear;
      tq.SQL.Add('Update Contratante set Ultima_Senha=:a where cgc=:b');
      tq.params[0].asstring := 'True';
      tq.params[1].asstring := xcgc;
      tq.execsql;
      result := true
    end else
      result := false;
    sh.free;
    tq.close;
    tq.free;
    dmint.db.Commit;
  except
    dmint.db.Rollback;
  end;
end;

function Tsenha.ver_data: byte;
var
  data_atual: tdatetime;
  tq, tq1: TQuery;
  xdata, xdia: string;
  xcgc: string;
  ano, mes, dia: word;
  check: Double;
begin
  dmint.db.StartTransaction;
  result := 0;
  try
    data_atual := date;
    tq1 := tquery.create(dmint);
    tq1.databasename := dmint.db.databasename;
    tq1.active := False;
    tq1.SQL.clear;
    tq1.Params.clear;
    tq1.SQL.Add('Select * from Contratante');
    tq1.open;
    xcgc := tq1.FieldByName('cgc').asstring;
    xdia := tq1.FieldByName('dias_aviso').asstring;
    DecodeDate(tq1.FieldByName('data_exp').asdatetime, ano, mes, dia);
    mes := mes + tq1.FieldByName('meses').asinteger;
    if mes > 12 then begin
      ano := ano + 1;
      mes := mes - 12;
    end;
    xdata := tq1.FieldByName('dia_fixo').asstring + '/' + strzero(mes, 2) + '/' + strzero(ano, 4);
    check := ((tq1.FieldByName('data_exp').asdatetime + 13) * 2) - 13;
    if not tq1.Eof then begin
      if (data_atual >= strtodate(xdata)) or (tq1.FieldByName('checkSum').AsFloat <> check) then begin
        tq := tquery.create(dmint);
        tq.databasename := dmint.db.databasename;
        tq.active := False;
        tq.SQL.clear;
        tq.Params.clear;
        tq.SQL.Add('Update Contratante set Ultima_Senha=:a where cgc=:b');
        tq.params[0].asstring := 'False';
        tq.params[1].asstring := xcgc;
        tq.execsql;
        tq.close;
        tq.free;
        tq1.Close;
        tq1.free;
        result := 0;
        dmint.db.Commit;
        exit;
      end else begin
        if data_atual >= (strtodate(xdata) - strtoint(xdia)) then
          dias := floattostr(strtodate(xdata) - data_atual);
      end;
      if data_atual < tq1.FieldByName('data_atual').asdatetime then begin
        tq := tquery.create(dmint);
        tq.databasename := dmint.db.databasename;
        tq.active := False;
        tq.SQL.clear;
        tq.Params.clear;
        tq.SQL.Add('Update Contratante set Ultima_Senha=:a where cgc=:b');
        tq.params[0].asstring := 'False';
        tq.params[1].asstring := xcgc;
        tq.execsql;
        tq.close;
        tq.free;
        result := 1;
      end else begin
        tq := tquery.create(dmint);
        tq.databasename := dmint.db.databasename;
        tq.active := False;
        tq.SQL.clear;
        tq.Params.clear;
        tq.SQL.Add('Update Contratante set data_atual=:a where cgc=:b');
        tq.Params[0].AsDateTime := data_atual;
        tq.params[1].asstring := xcgc;
        tq.execsql;
        tq.close;
        tq.free;
        result := 2;
      end;
    end else
      result := 2;
    tq1.close;
    tq1.free;
    dmint.db.Commit;
  except
    dmint.db.Rollback;
  end;
end;

function Tsenha.altera_data_exp;
var
  tq: TQuery;
  xcgc: string;
begin
  dmint.db.StartTransaction;
  try
    contratante := TContratante.Create;
    contratante.Recuperar;
    xcgc := contratante.cgc;
    contratante.free;
    tq := tquery.create(dmint);
    tq.databasename := dmint.db.databasename;
    tq.active := False;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Contratante set data_exp=:a where cgc=:b');
    tq.params[0].asdatetime := date;
    tq.params[1].asstring := xcgc;
    tq.execsql;
    tq.close;
    tq.free;
    altera_checkSum;
    dmint.db.Commit;
    result := true;
  except
    dmint.db.Rollback;
    result := false;
  end;
end;

procedure TSenha.Cria_Chave_Registro;
var
  Reg: TRegistry;
  val: TStringList;
  i: integer;
  achou: boolean;
begin
  Reg := TRegistry.Create;
  val := TStringList.Create;
  achou := false;
  try
    Reg.RootKey := HKey_Local_Machine;
    if Reg.OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion', True) then begin
      reg.GetValueNames(val);
      for i := 0 to val.count - 1 do if Val.Strings[I] = 'Ind_System' then achou := true;
      if not achou then reg.WriteString('Ind_System', 'True');
    end;
  finally
    val.free;
    reg.CloseKey;
    Reg.Free;
  end;
end;

function TSenha.Verifica_Chave_Registro;
var
  Reg: TRegistry;
  val: TStringList;
  i: integer;
begin
  Reg := TRegistry.Create;
  val := TStringList.Create;
  result := false;
  try
    Reg.RootKey := HKey_Local_Machine;
    if Reg.OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion', True) then begin
      reg.GetValueNames(val);
      for i := 0 to val.count - 1 do if Val.Strings[I] = 'Ind_System' then result := true;
    end;
  finally
    val.free;
    reg.CloseKey;
    Reg.Free;
  end;
end;

function Tsenha.altera_data_atual;
var
  tq: TQuery;
begin
  tq := tquery.create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update contratante set data_atual=:a');
  tq.params[0].asdatetime := date;
  try
    tq.execsql;
    result := true;
  except
    result := false;
  end;
  tq.close;
  tq.free;
end;

function Tsenha.altera_checkSum: boolean;
var
  tq: TQuery;
begin
  tq := tquery.create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update contratante set checkSum=:a');
  tq.params[0].asFloat := ((date + 13) * 2) - 13;
  try
    tq.execsql;
    result := true;
  except
    result := false;
  end;
  tq.close;
  tq.free;
end;

function Tsenha.grava_CheckSum: boolean;
var
  tq: TQuery;
  Data: TDateTime;
begin
  tq := tquery.create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select checkSum from Contratante');
  tq.Open;
  if tq.FieldByName('CheckSum').IsNull then begin
    Result := True;
    tq.active := False;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Select Data_Exp from Contratante');
    tq.Open;
    Data := tq.FieldByName('Data_Exp').AsDateTime;
    tq.active := False;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update Contratante set CheckSum =:a');
    tq.Params[0].AsFloat := ((Data + 13) * 2) - 13;
    tq.ExecSQL;
  end else Result := False;
  tq.Close;
  tq.Free;
end;

end.
